package com.chaoxuan.uebt.activity;

import android.app.Dialog;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.text.Html;
import android.util.Log;
import android.view.View;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.chaoxuan.uebt.R;
import com.chaoxuan.uebt.model.Balance;
import com.chaoxuan.uebt.model.Store;
import com.chaoxuan.uebt.model.StoreOrder;
import com.chaoxuan.uebt.model.User;
import com.chaoxuan.uebt.utils.BaseSubscriber;
import com.chaoxuan.uebt.utils.CommomDialog2;
import com.chaoxuan.uebt.utils.CommomDialog3;
import com.chaoxuan.uebt.utils.Const;
import com.chaoxuan.uebt.utils.MyWebViewClient;
import com.chaoxuan.uebt.utils.RetrofitUtils;
import com.chaoxuan.uebt.utils.CacheUtils;

public class StoreDetailActivity extends BaseActivity implements View.OnClickListener {
    Store store;
    Balance balance;
    String id;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_store_detail);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            getWindow().setStatusBarColor(Color.parseColor("#F5F6FA"));
        }
        initTitle();
        //findViewById(R.id.btn_confirm).setOnClickListener(this);
        id= getIntent().getStringExtra("id");
        subscribe(RetrofitUtils.getAPIService().storeDetail(id), new BaseSubscriber<Store>() {
            @Override
            public void onSuccess(Store data) {
                store=data;
                show(data);
            }
        });
        User user= CacheUtils.getInstance(this).getUser();
        if(user!=null)
        subscribe(RetrofitUtils.getAPIService().getUserBalance(user.getToken(),Const.DEVICE_TYPE), new BaseSubscriber<Balance>() {
            @Override
            public void onSuccess(Balance data) {
                balance=data;
            }
        });
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.btn_confirm:
                if(store.getType()==1){//1：实物商品；
                    Intent i= new Intent(this,Store1Activity.class);
                    i.putExtra("store",store);
                    startActivity(i);
                }else{//2：虚拟商品
                    confirm();
                }
                break;
        }
    }

    private void show(Store s){
        ImageView img=(ImageView) findViewById(R.id.img_logo);
        TextView tvTitle=(TextView) findViewById(R.id.tv_store_title);
        TextView tvBalance=(TextView) findViewById(R.id.tv_store_balance);
        TextView tvPay=(TextView) findViewById(R.id.tv_store_pay);

        WebView webView=(WebView) findViewById(R.id.webview);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setDefaultTextEncodingName("UTF -8");//设置默认为utf-8
        webView.setWebViewClient(new MyWebViewClient());

        Glide.with(getBaseContext()).load(s.getImg()).diskCacheStrategy(DiskCacheStrategy.ALL).placeholder(R.mipmap.default_info).error(R.mipmap.default_info).into(img);
        tvTitle.setText(s.getName());
        tvBalance.setText("库存:"+s.getBalance()+"/"+s.getStock());
        tvPay.setText(s.getPay());

        String content=s.getContent().replaceAll("src=\"","src=\"http://www.chaoxuan.io");
        Log.d("html",content);
        webView.loadData(content, "text/html; charset=UTF-8", null);
    }

    private void confirm(){
        if(store==null)return;
        String content1="商品名称:"+store.getName();
        String content2="购买数量:1";
        String content3="应付积分:"+store.getPay();
        String content4="账户余额:"+((balance==null)?"0.00":balance.getBalance());
        if(Double.parseDouble(store.getPay())>Double.parseDouble(balance.getBalance())) {
            new CommomDialog2(this,R.style.dialog,"余额不足以支付该商品").setTitle("余额不足").show();
            return;
        }
        new CommomDialog3(this, R.style.dialog, content1, content2, content3, content4, new CommomDialog3.OnCloseListener() {
            @Override
            public void onClick(Dialog dialog, boolean confirm) {
                if(confirm){
                    dialog.dismiss();
                    payNetwork();
                }
            }
        }).show();
    }

    private void payNetwork(){
        User user= CacheUtils.getInstance(this).getUser();
        subscribe(RetrofitUtils.getAPIService().payNetwork(user.getToken(),id, Const.DEVICE_TYPE), new BaseSubscriber<StoreOrder>() {
            @Override
            public void onSuccess(StoreOrder o) {
                Intent i= new Intent(getBaseContext(),StoreSuccessActivity.class);
                i.putExtra("title",store.getName());
                i.putExtra("type",2);
                i.putExtra("pay",store.getPay());
                i.putExtra("id",o.getOid());
                startActivity(i);
            }

        });
    }
}
